package com.leon.androidcoursedesign.ui.activitys.toolBoxActivity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.ui.activitys.BaseActivity;
import com.leon.androidcoursedesign.utils.CalculateUtils;

import java.lang.reflect.Method;

public class CalActivity extends BaseActivity {
    private EditText tool_box_cal_editText;
    private TextView tool_box_cal_textView_result;

    private CalculateUtils calculateUtils;


    @Override
    public int setLayout() {
        return R.layout.activity_cal;
    }

    @Override
    public void initView() {
        tool_box_cal_editText = findViewById(R.id.tool_box_cal_editText);
        tool_box_cal_editText.setFocusable(true);
        tool_box_cal_editText.setFocusableInTouchMode(true);

        tool_box_cal_textView_result = findViewById(R.id.tool_box_cal_textView_result);

        // 不显示软键盘
        Class<EditText> cls = EditText.class;
        Method method;
        try {
            method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(tool_box_cal_editText, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initValue() {
        this
                .setOnClick(R.id.tool_box_cal_imageView_back)
                .setOnClick(R.id.tool_box_cal_imageView_delete)
                .setOnClick(R.id.tool_box_cal_button_reset)
                .setOnClick(R.id.tool_box_cal_button_l_brackets)
                .setOnClick(R.id.tool_box_cal_button_r_brackets)
                .setOnClick(R.id.tool_box_cal_button_equal)
                .setOnClick(R.id.tool_box_cal_button_divide)
                .setOnClick(R.id.tool_box_cal_button_seven)
                .setOnClick(R.id.tool_box_cal_button_eight)
                .setOnClick(R.id.tool_box_cal_button_nine)
                .setOnClick(R.id.tool_box_cal_button_mul)
                .setOnClick(R.id.tool_box_cal_button_four)
                .setOnClick(R.id.tool_box_cal_button_five)
                .setOnClick(R.id.tool_box_cal_button_six)
                .setOnClick(R.id.tool_box_cal_button_sub)
                .setOnClick(R.id.tool_box_cal_button_one)
                .setOnClick(R.id.tool_box_cal_button_two)
                .setOnClick(R.id.tool_box_cal_button_three)
                .setOnClick(R.id.tool_box_cal_button_add)
                .setOnClick(R.id.tool_box_cal_button_zero)
                .setOnClick(R.id.tool_box_cal_button_point);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tool_box_cal_imageView_back:
                this.finish();
                break;
            case R.id.tool_box_cal_button_equal:
                getResult(tool_box_cal_editText.getText().toString());
                break;
            case R.id.tool_box_cal_imageView_delete:
                if (!getEditTextViewString().equals("")) {
                    deleteEditValue(getEditSelection());
                }
                break;
            case R.id.tool_box_cal_button_reset:
                clearText();
                tool_box_cal_textView_result.setText("");
                break;
            case R.id.tool_box_cal_button_l_brackets:
                insertString("(");
                break;
            case R.id.tool_box_cal_button_r_brackets:
                insertString(")");
                break;
            case R.id.tool_box_cal_button_divide:
                insertString("÷");
                break;
            case R.id.tool_box_cal_button_mul:
                insertString("×");
                break;
            case R.id.tool_box_cal_button_sub:
                insertString("-");
                break;
            case R.id.tool_box_cal_button_add:
                insertString("+");
                break;
            case R.id.tool_box_cal_button_nine:
                insertString("9");
                break;
            case R.id.tool_box_cal_button_eight:
                insertString("8");
                break;
            case R.id.tool_box_cal_button_seven:
                insertString("7");
                break;
            case R.id.tool_box_cal_button_six:
                insertString("6");
                break;
            case R.id.tool_box_cal_button_five:
                insertString("5");
                break;
            case R.id.tool_box_cal_button_four:
                insertString("4");
                break;
            case R.id.tool_box_cal_button_three:
                insertString("3");
                break;
            case R.id.tool_box_cal_button_two:
                insertString("2");
                break;
            case R.id.tool_box_cal_button_one:
                insertString("1");
                break;
            case R.id.tool_box_cal_button_zero:
                insertString("0");
                break;
            case R.id.tool_box_cal_button_point:
                insertString(".");
                break;
            default:
                break;
        }
    }

    /**
     * 获取算式结果
     *
     * @param formula   算式
     */
    private void getResult(String formula) {
        if (calculateUtils == null) {
            calculateUtils = new CalculateUtils();
        }

        if (formula.equals("")) {
            tool_box_cal_textView_result.setText("");
            return;
        }

        String formulaResult = calculateUtils.calFormula(formula);
        tool_box_cal_textView_result.setText(formulaResult);
    }

    /**
     * 添加文字
     *
     * @param sequence  要添加的文字
     */
    private void insertString (String sequence) {
        int index = getEditSelection();

        if (index < 0 || index >= getEditTextViewString().length()) {
            tool_box_cal_editText.append(sequence);
        } else {
            // 在光标位置添加
            tool_box_cal_editText.getEditableText().insert(index, sequence);
        }
    }

    // 获取光标位置
    private int getEditSelection() {
        return tool_box_cal_editText.getSelectionStart();
    }

    // 获取editText文字内容
    private String getEditTextViewString () {
        return tool_box_cal_editText.getText().toString();
    }

    // 清空editText的文字内容
    private void clearText() {
        tool_box_cal_editText.getText().clear();
    }

    // 删除指定位置的文字
    private void deleteEditValue(int index) {
        tool_box_cal_editText.getText().delete(index - 1, index);
    }
}